package com.xin.ruanjian.omsystem.utils;

import com.xin.ruanjian.omsystem.bean.DishBean;
import com.xin.ruanjian.omsystem.model.ShopCarModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.realm.Realm;
import io.realm.RealmAsyncTask;
import io.realm.RealmResults;

/**
 * Created by local123 on 2018/2/23.
 */

public class ShopCarUtils {
    public static Realm realm = Realm.getDefaultInstance();

    public static void updateShopCar(ShopCarModel model, final int num, int userId) {
        final ShopCarModel query = realm.where(ShopCarModel.class)
                .equalTo("userId", userId)
                .equalTo("mid", model.getMid())
                .equalTo("did", model.getDid())
                .findFirst();
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                if (num == 0) {
                    query.deleteFromRealm();
                }else {
                    query.setdNum(num);
                }
            }
        });
    }

    public static void updateShopCar(final DishBean bean, final int num, final int userId) {
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                ShopCarModel query = realm.where(ShopCarModel.class)
                        .equalTo("userId", userId)
                        .equalTo("mid", bean.getMid())
                        .equalTo("did", bean.getDid())
                        .findFirst();
                //查找失败，插入新的购物车数据
                if (query == null) {
                    ShopCarModel shopCarModel = realm.createObject(ShopCarModel.class, bean.getDid());
                    shopCarModel.setMid(bean.getMid());
                    shopCarModel.setShopId(bean.getShopid());
                    shopCarModel.setdName(bean.getDname());
                    shopCarModel.setdPic(bean.getDpic());
                    shopCarModel.setdPrice(bean.getDprice());
                    shopCarModel.setdNum(1);
                    shopCarModel.setUserId(userId);
                    shopCarModel.setAddTime(System.currentTimeMillis());
                }else {
                    if (num == 0) {
                        query.deleteFromRealm();
                    }else {
                        query.setdNum(num);
                    }
                }
            }
        });
    }

    public static List<ShopCarModel> getShopCarData(int shopId, int userId) {
        RealmResults<ShopCarModel> query = realm.where(ShopCarModel.class)
                .equalTo("userId", userId)
                .equalTo("shopId", shopId)
                .findAll();
        List<ShopCarModel> data = new ArrayList<>();
        data.addAll(query);
       return data;
    }

    public static void clearShopCarData(final int shopId, final int userId) {
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                RealmResults<ShopCarModel> query = realm.where(ShopCarModel.class)
                        .equalTo("userId", userId)
                        .equalTo("shopId", shopId)
                        .findAll();
                for (ShopCarModel model : query) {
//                    model.setdNum(0);
                    model.deleteFromRealm();
                }
            }
        });
    }

    public static Map<String, String> getShopCarInfo(int shopId, int userId) {
        List<ShopCarModel> shopCarData = getShopCarData(shopId, userId);
        Map<String, String> info = new HashMap<>();
        double allPrice = 0;
        int num = 0;
        for (ShopCarModel model : shopCarData) {
            allPrice += (model.getdPrice() * model.getdNum());
            num += model.getdNum();
        }
        info.put("price", DoubleUtils.round(allPrice, 2)+"");
        info.put("num", num + "");
        return info;
    }

    public static double getShopCarAllPrice(List<ShopCarModel> shopCarData) {
        double allPrice = 0;
        for (ShopCarModel model : shopCarData) {
            allPrice += (model.getdPrice() * model.getdNum());
        }
        return DoubleUtils.round(allPrice, 1);
    }
}
